package com.xiaoma.tpo.service;

import com.xiaoma.tpo.dao.LiveListDao;
import com.xiaoma.tpo.domain.LiveList;
import com.xiaoma.tpo.dto.LiveListDto;
import com.xiaoma.tpo.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Owner on 2015/5/15.
 */
@Service
public class LiveListService {
    @Autowired
    private LiveListDao liveListDao;
    SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    public Pager getList(int page,int pageSize){
        if(page==0){
            page=1;
        }
        Pager pager = liveListDao.getList(page,pageSize);
        return pager;
    }

    public boolean addLive(LiveListDto dto){
        LiveList ll = new LiveList();
        ll.setId(dto.getId());
        ll.setLiveId(dto.getLiveId());
        ll.setLiveTitle(dto.getLiveTitle());
        try {
            ll.setStartTime(sf.parse(dto.getStartTime()));
            ll.setEndTime(sf.parse(dto.getEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ll.setStatus(0);
        liveListDao.insert(ll);
        return true;
    }

    public boolean editLive(LiveListDto dto){
        LiveList ll = new LiveList();
        ll.setId(dto.getId());
        ll.setLiveId(dto.getLiveId());
        ll.setLiveTitle(dto.getLiveTitle());
        try {
            ll.setStartTime(sf.parse(dto.getStartTime()));
            ll.setEndTime(sf.parse(dto.getEndTime()));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        liveListDao.update(ll);
        return true;
    }

    public LiveList getCurrentLive(){
        LiveList ll = new LiveList();
        ll.setStatus(1);
        List<LiveList> list = liveListDao.findByColumns(ll);
        if(list.size()>0){
            ll = list.get(0);
            return ll;
        }else{
            return  null;
        }

    }
}
